package ar.edu.unq.tpi.construi.c1.tejeda.UI;

import java.text.SimpleDateFormat;
import java.util.Arrays;

import org.uqbar.arena.actions.MessageSend;
import org.uqbar.arena.layout.ColumnLayout;
import org.uqbar.arena.widgets.Button;
import org.uqbar.arena.widgets.Label;
import org.uqbar.arena.widgets.Panel;
import org.uqbar.arena.widgets.Selector;
import org.uqbar.arena.widgets.TextBox;
import org.uqbar.arena.widgets.tables.Column;
import org.uqbar.arena.widgets.tables.Table;
import org.uqbar.arena.windows.Dialog;
import org.uqbar.arena.windows.WindowOwner;
import org.uqbar.commons.model.Search;
import org.uqbar.commons.model.SearchByExample;


import com.uqbar.commons.collections.Transformer;

import ar.edu.unq.tpi.constriu.c1.tejeda.Dominio.Agenda.Agenda;
import ar.edu.unq.tpi.constriu.c1.tejeda.Dominio.Agenda.Contact;

@SuppressWarnings("serial")
public class SearchContactWindow extends SearchWindow<Contact, SearchByExample<Contact>> {
	
	/**
	 * 
	 */

	@SuppressWarnings("static-access")
	public SearchContactWindow(WindowOwner owner) {
		super(owner, new SearchByExample<Contact>(Agenda.getInstance().getHome(Contact.class)));
	}

	@Override
	protected void createMainTemplate(Panel formBuilder) {
		this.setTitle("Buscardor de Contactos");
		this.setTaskDescription("Ingrese los parámetros de búsqueda");
		super.createMainTemplate(formBuilder);
	}

	@Override
	protected Panel createMainPanel() {
		Panel formBuilder = super.createMainPanel();
		formBuilder.setWidth(1200);
		return formBuilder;
	}

	@Override
	protected void createFormPanel(Panel mainPanel) {
		Panel searchFormPanel = new Panel(mainPanel);
		searchFormPanel.bindContents(SearchByExample.EXAMPLE);
		searchFormPanel.setLayout(new ColumnLayout(4));

		// Field nombre
		new Label(searchFormPanel).setText("Name");
		new TextBox(searchFormPanel).bindValueToProperty(Contact.NAME);
				
		//Field teléfono
		new Label(searchFormPanel).setText("Phone number");
		new TextBox(searchFormPanel).bindValueToProperty(Contact.PHONENUMBER);
			
		// Field mail
		new Label(searchFormPanel).setText("Mail");
		new TextBox(searchFormPanel).bindValueToProperty(Contact.MAIL);	
		
		//Field ciudad
		new Label(searchFormPanel).setText("City");
		new Selector(searchFormPanel).setContents(Arrays.asList(Contact.City.values()), "nombre").bindValueToProperty(Contact.CITY);
	}

	
	
	@Override
	protected void describeResultsGrid(Table<Contact> table) {
		Column<Contact> nombreColumn = new Column<Contact>(table);
		nombreColumn.setTitle("Name");
		nombreColumn.setPreferredSize(100);
		nombreColumn.bindContentsToProperty(Contact.NAME);
		

		Column<Contact> direccionColumn = new Column<Contact>(table);
		direccionColumn.setTitle("Mail");
		direccionColumn.setPreferredSize(100);
		direccionColumn.bindContentsToProperty(Contact.MAIL);
		
		Column<Contact> phoneColumn = new Column<Contact>(table);
		phoneColumn.setTitle("Phone number");
		phoneColumn.setPreferredSize(100);
		phoneColumn.bindContentsToProperty(Contact.PHONENUMBER);
		
		Column<Contact> birthDateColumn = new Column<Contact>(table);
		birthDateColumn.setTitle("Birthdate");
		birthDateColumn.setPreferredSize(100);
		birthDateColumn.bindContentsToTransformer(new Transformer<Contact, String>() {
			@Override
			public String transform(Contact contact) {
				return new SimpleDateFormat("dd/MM/yyyy").format(contact.getBirthDate());
			}
		});
		
		Column<Contact> observationsColumn = new Column<Contact>(table);
		observationsColumn.setTitle("Observations");
		observationsColumn.setPreferredSize(100);
		observationsColumn.bindContentsToProperty(Contact.OBSERVATIONS);
		
		Column<Contact> cityColumn = new Column<Contact>(table);
		cityColumn.setTitle("City");
		cityColumn.setPreferredSize(100);
		cityColumn.bindContentsToProperty(Contact.CITY);
		
	}

	@Override
	protected void addActions(Panel actionsPanel) {
		super.addActions(actionsPanel);
		Button nuevoCto = new Button(actionsPanel);
		nuevoCto.setCaption("Nuevo Contacto");
		nuevoCto.onClick(new MessageSend(this, "crearContacto"));
	}

	public void crearContacto() {
		Dialog<?> crearCto = new CreateContacDialog(this);
		crearCto.onAccept(new MessageSend(this.getModel(), Search.SEARCH));
		crearCto.open();
	}

	@Override
	protected Dialog<?> createEditor(Contact selected) {
		return new ModifyContactDialog(this, selected);
	}
	
	

	
}